package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.NavigationMapper;
import com.cn.medicalcare.mapper.ProductInfoMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Navigation;
import com.cn.medicalcare.pojo.ProductAllInfo;
import com.cn.medicalcare.pojo.ProductInfo;
import com.cn.medicalcare.service.ProductInfoService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.function.Function;

public class ProductInfoServiceImpl implements ProductInfoService {

    public static final String PRODUCT_CACHE_KEY = "product_all_info_by_id_";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final ProductInfoServiceImpl instance = new ProductInfoServiceImpl();
    }

    private ProductInfoServiceImpl() {
    }

    public static ProductInfoServiceImpl getInstance() {
        return ProductInfoServiceImpl.SingletonClassInstance.instance;
    }

    @Override
    public ProductInfo selectProductInfoByProduct_id(int product_id) {
        return SqlSessionFactoryTool.DoMapper(ProductInfoMapper.class, m -> m.selectProductInfoByProduct_id(product_id));
    }

    @Override
    public boolean insertProductInfo(ProductInfo productInfo) {
        return SqlSessionFactoryTool.DoMapperAndCommit(ProductInfoMapper.class, m -> m.insertProductInfo(productInfo));
    }

    @Override
    public boolean updateProductInfo(ProductInfo productInfo) {
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(ProductInfoMapper.class, m -> m.updateProductInfo(productInfo));
        String cachekey = PRODUCT_CACHE_KEY + productInfo.getProduct_id();
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(cachekey));
        }
        return updateFlag;
    }

    @Override
    public ProductAllInfo selectProductAllInfoById(int id) {
        ProductAllInfo productAllInfo = null;
        String cachekey = PRODUCT_CACHE_KEY + id;
        try {
            String productAllInfojson = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(productAllInfojson)) {
                JedisUtil.jedisDo(j -> j.expire(cachekey, JedisUtil.CACHE_TIME));
                productAllInfo = JsonTool.toObject(productAllInfojson, ProductAllInfo.class);
            }
        } catch (Exception e) {
        }
        if (productAllInfo == null) {
            productAllInfo = SqlSessionFactoryTool.DoMapper(ProductInfoMapper.class, m -> m.selectProductAllInfoById(id));
            try {
                String productAllInfojson = JsonTool.toJson(productAllInfo);
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, productAllInfojson));
            } catch (Exception e) {
            }
        }
        return productAllInfo;
    }

}
